/*
 * Created on Feb 20, 2004
 *
 * Author: Sandeep Prakash
 * Project: Sucxentv2 (using leaf nodes only)
 */
package dbsucxentW.storageEdgeW.loadersW.relationalW;

import java.io.File;
import java.sql.Connection;

import javax.xml.parsers.SAXParserFactory;

import org.xml.sax.helpers.DefaultHandler;

import dbsucxentW.storageEdgeW.entitiesW.LeafNodeCollection;
import dbsucxentW.storageEdgeW.entitiesW.PathCollection;
import dbsucxentW.storageEdgeW.loadersW.XmlShredder;
//klarinda 20060823 - never used, import dbsucxent.storageEdge.parser.BranchDataSummarizer;
import dbsucxentW.storageEdgeW.parserW.BranchOrderDecorator;
import dbsucxentW.storageEdgeW.parserW.BufferedLeafNodeCollection;

/**
 * @author Administrator
 *
 * 
 */
public class ThreadedRelationalXmlShredder extends XmlShredder {

	protected String _dbName;	//klarinda 20060905

	/**
	 * @param file
	 */
	//klarinda 20060905 include _dbName
	//public ThreadedRelationalXmlShredder(String file, Connection dbConnection) {
	public ThreadedRelationalXmlShredder(String file, Connection dbConnection, String dbName) {
		super(file);
		_dbConnection = dbConnection;
		_dbName = dbName;	//klarinda 20060905
	}
	
	/*
	 * Same - except for BufferedLeafNodeCollection.
	 * @see dbsucxent.storageEdge.loaders.XmlShredder#initParser()
	 */
	//new area
	protected void initParser() throws Exception{

		System.out.println( "\n\nThreadedRelationalXmlShredder.initParser()");//klarinda 20060822

		_leafCollector = new BufferedLeafNodeCollection();
		//
		//Load url provider from config file
		//
		
		
		//klarinda 20060905 add _dbName, 20070525 add _file
		//_store = new RelationalStore( urlProvider, _dbConnection );
		_leafCollector.setFileName( _file );
		
		BranchOrderDecorator summarizer = new BranchOrderDecorator(_leafCollector, _store);
	
		System.out.println( "\n\nThreadedRelationalXmlShredder.initParser() parse the file");//klarinda 20060823
		
		javax.xml.parsers.SAXParser parser = SAXParserFactory.newInstance().newSAXParser();
		parser.parse( new File( _file ), summarizer );
		//System.out.println("trying summarizer");
		
		/* klarinda 20060901 - DocumentSummary not used in Edge 
		System.out.println( "\n\nThreadedRelationalXmlShredder.initParser() get the summary");//klarinda 20060823
		_summary = summarizer.getSummary();
		_leafCollector.setSummary( _summary );
		*/
		summarizer = null;
		
		super.initParser();
		
		//System.out.println("done ok2");
	}

	/* This is threaded. The rest of the methods are the same.
	 */
	//klarinda 20060823 - I believe this function is never called
	public void parse() throws Exception {
		System.out.println( "\n\nThreadedRelationalXmlShredder.parse()");	//klarinda 20060822
		
		/* klarinda 20060823 - I believe this function is never called
		 * 
		_parser = new BranchDataSummarizer( _leafCollector, _summary );
		
		ThreadedParser parser = new ThreadedParser( _file, _parser );
		
		//Start new thread.
		Thread thrd = new Thread( parser );
		thrd.start();
		
		*/
	}

	/* Same. maybe, move it up the heirarchy - later.
	 */
	public void insert() throws Exception {
		System.out.println( "\n\nThreadedRelationalXmlShredder.insert()");	//klarinda 20060822
		//System.out.println("trying to store");
		_store.store( _leafCollector );
		//System.out.println("done ok2");
		_store.complete();
	}
	
	class ThreadedParser implements Runnable{
		
		private DefaultHandler _handler;
		private String _file;
		
		public ThreadedParser( String file, DefaultHandler handler ){
			_file	 = file;
			_handler = handler;
		}
		
		public void run() {
			
			try{
				javax.xml.parsers.SAXParser parser = SAXParserFactory.newInstance().newSAXParser();
				parser.parse( new File( _file ), _parser );
				
				System.out.println( "Parsing is complete.." );
				
			}catch( Exception ex ){
				ex.printStackTrace();
			}
		}

	}

}
